package cn.cs.service.impl;

import cn.cs.client.CsFactoryClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.CsFactory;
import cn.cs.service.CsFactoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CsFactoryServiceImpl implements CsFactoryService {

    @Resource
    private CsFactoryClient csFactoryClient;

    public CsFactory getCsFactoryById(String id) throws Exception {
        return csFactoryClient.getCsFactoryById(id);
    }

    public CsFactory getCsFactoryByUserName(String username) throws Exception {
        return csFactoryClient.getCsFactoryByUserName(username);
    }


    public List<CsFactory> getCsFactoryListByMap(Map<String, Object> param) throws Exception {
        return csFactoryClient.getCsFactoryListByMap(param);
    }

    public Integer getCsFactoryCountByMap(Map<String, Object> param) throws Exception {
        return csFactoryClient.getCsFactoryCountByMap(param);
    }

    public Integer addCsFactory(CsFactory csFactory) throws Exception {
        csFactory.setCreateDate(new Date());
        return csFactoryClient.insertCsFactory(csFactory);
    }

    public Integer modifyCsFactory(CsFactory csFactory) throws Exception {
        csFactory.setModifyDate(new Date());
        return csFactoryClient.updateCsFactory(csFactory);
    }

    public Integer deleteCsFactoryById(String id) throws Exception {
        return csFactoryClient.deleteCsFactoryById(id);
    }

    public Page<CsFactory> queryCsFactoryPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = csFactoryClient.getCsFactoryCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<CsFactory> csFactoryList = csFactoryClient.getCsFactoryListByMap(param);
        page.setRows(csFactoryList);
        return page;
    }

}
